package com.dayuanit.booksystem.servlet;

import com.dayuanit.booksystem.domain.PublisherInfo;
import com.dayuanit.booksystem.eneity.Book;
import com.dayuanit.booksystem.eneity.Publisher;
import com.dayuanit.booksystem.service.PublisherService;
import com.dayuanit.booksystem.service.impl.PublisherServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 处理出版社的servlet
 */
public class PublisherServlet extends BaseServlet {

    private PublisherService publisherService = new PublisherServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println(Thread.currentThread().getName());
        System.out.println(req.getClass().getName());//org.apache.catalina.connector.RequestFacade
        System.out.println(resp.getClass().getName());//org.apache.catalina.connector.ResponseFacade
        System.out.println(">>>>>>>******PublisherServlet*****455544*******<<<<<<<<<<<<  url=" + req.getRequestURL().toString());

        //根据action的值，来确定使用哪个service的哪个方法。
        String action = req.getParameter("action");

        //填加出版社
        if ("addPublisher".equals(action)) {
            add(req, resp);
        }

        //查询出版社
        if ("queryPubblisherById".equals(action)) {
            queryById(req, resp);
        }

        //更新出版社
        if ("updatePublisherAddress".equals(action)) {
          updateAddress(req, resp);
        }

        //获取出版社信息
        if ("loadPublisherInfo".equals(action)) {
//            loadPublisherInfo(req, resp);
            loadPublisherInfo2(req, resp);
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println(">>>>>>>>>>");
    }

    private void queryById(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
        String publiserId = req.getParameter("publiserId");
        Publisher publisher = publisherService.queryPublisher(Long.parseLong(publiserId));

        req.setAttribute("publisher", publisher);
        req.getRequestDispatcher("/page/v2/publisher.jsp").forward(req, resp);
    }

    public void add(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
        String name = req.getParameter("name");
        String address = req.getParameter("address");
        publisherService.addPublisher(name, address);

        sendResultToClient("添加成功！", resp);
    }

    public void updateAddress(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String publisherId = req.getParameter("publisherId");
        String address = req.getParameter("address");
        publisherService.updatePublisherAddress(Long.parseLong(publisherId),address);
        sendResultToClient("更新成功！", resp);
    }

    public void loadPublisherInfo2(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println(">>>>>>>loadPublisherInfo2");
        String publisherId = req.getParameter("publisherId");
        PublisherInfo publisherInfo = publisherService.loadPublisherInfo(Long.parseLong(publisherId));

        //需要在jsp模板填充的内容，可以通过setAttribute方法进行设置，然后再jsp模板中取出即可。
        req.setAttribute("xxxxxxxxxxxxxx", publisherInfo);
        //响应阶段 响应一个模板页面
        req.getRequestDispatcher("/page/publisherInfo.jsp").forward(req, resp);
    }

    public void loadPublisherInfo(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String publisherId = req.getParameter("publisherId");

        PublisherInfo publisherInfo = publisherService.loadPublisherInfo(Long.parseLong(publisherId));

        String html = "<html>\n" +
                "<head>\n" +
                "    <meta charset=\"UTF-8\">\n" +
                "    <title>大猿图书管理系统</title>\n" +
                "</head>\n" +
                "\n" +
                "<body>\n" +
                "\n" +
                "    出版社名称：<span>"+publisherInfo.getPublisherName()+"</span>\n" +
                "    <br>\n" +
                "    出版社地址：<span>"+publisherInfo.getPublisherAddress()+"</span>\n" +
                "    <table border=\"1\">\n" +
                "        <tr>\n" +
                "            <td>书名</td>\n" +
                "            <td>作者</td>\n" +
                "            <td>价格</td>\n" +
                "            <td>出版社ID</td>\n" +
                "        </tr>\n";

        for (Book book : publisherInfo.getBooks()) {
            html +=   "        <tr>\n" +
                    "            <td>"+book.getBookName()+"</td>\n" +
                    "            <td>"+book.getAuthor()+"</td>\n" +
                    "            <td>"+book.getPrice()+"</td>\n" +
                    "            <td>"+book.getPublisherId()+"</td>\n" +
                    "        </tr>\n" +
                    "\n" ;
        }

            html +=    "    </table>\n" +
                "\n" +
                "</body>\n" +
                "\n" +
                "<footer>\n" +
                "\n" +
                "</footer>\n" +
                "</html>";
        sendResultToClient(html, resp);
    }


}
